package com.yango.teach.controller.front;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yango.commonutil.result.Result;
import com.yango.commonutil.utils.JwtUtils;
import com.yango.teach.client.OrderClient;
import com.yango.teach.entity.Course;
import com.yango.teach.entity.vo.course.*;
import com.yango.teach.service.ChapterService;
import com.yango.teach.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前台网站课程 前端控制器
 * </p>
 *
 * @author LiDingYin
 * @since 2021-07-24
 */
@Api(description = "课程 前端控制器")
@RestController
@RequestMapping("/teach/front/course")
public class CourseFrontController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    //课程分类列表
    @ApiOperation(value = "课程分类列表")
    @PostMapping("/pageList/{current}/{limit}")
    public Result pageList(@ApiParam(name = "current", value = "当前页", required = true)
                           @PathVariable long current,
                           @ApiParam(name = "limit", value = "每页显示记录数", required = true)
                           @PathVariable long limit,
                           @ApiParam(name = "courseFrontQuery", value = "课程查询对象", required = false)
                           @RequestBody CourseFrontQuery courseFrontQuery) {
        //创建page对象
        Page<Course> page = new Page<>(current, limit);
        //调用方法查询
        Map<String, Object> map = courseService.pageList(page, courseFrontQuery);
        //返回数据
        return Result.ok().data(map);
    }

    //根据课程id查询课程信息-用户界面
    @ApiOperation(value = "根据课程id查询课程信息")
    @GetMapping("/getById/{courseId}")
    public Result getById(@ApiParam(name = "courseId", required = true, value = "课程id")
                          @PathVariable String courseId,
                          HttpServletRequest request) {
        //根据课程id查询课程基本信息-用户界面
        CourseFrontVo courseFrontVo = courseService.getCourseById(courseId);

        //根据课程id查询课程章节小节
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);

        //查询是否购买课程
        boolean isBuyCourse = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return Result.ok().data("courseFrontVo", courseFrontVo)
                .data("chapterVoList", chapterVoList)
                .data("isBuyCourse", isBuyCourse);
    }

    //根据课程id获取课程信息-生成订单
    @ApiOperation(value = "根据课程id获取课程信息-生成订单")
    @GetMapping("/getCourse/{courseId}")
    public com.yango.serviceutil.vo.CourseFrontVo getCourse(@ApiParam(name = "courseId", value = "课程id", required = true)
                                                            @PathVariable String courseId) {
        CourseFrontVo course = courseService.getCourseById(courseId);//调用方法查询
        com.yango.serviceutil.vo.CourseFrontVo courseFrontVo =
                new com.yango.serviceutil.vo.CourseFrontVo();
        //将 course对象中的值复制到courseInfoForm中
        BeanUtils.copyProperties(course, courseFrontVo);
        //返回
        return courseFrontVo;
    }
}

